package com.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pinda.dao.SmsSample;
import com.pinda.dao.UUIDToDigits;
import com.pinda.mapper.PdMemberMapper;
import com.pinda.users.entity.PdMember;
import com.pinda.service.IPdMemberService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@Service
public class PdMemberServiceImpl extends ServiceImpl<PdMemberMapper, PdMember> implements IPdMemberService {

    @Resource
    private PdMemberMapper pdMemberMapper;

    @Override
    public PdMember queryMember(PdMember pdMember) throws Exception {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",pdMember.getPhone());

        PdMember pdMember1 = this.pdMemberMapper.selectOne(queryWrapper);
        String s = DigestUtils.md5DigestAsHex(pdMember.getPassword().getBytes());
        if (pdMember1==null){
            return null;
        }
        if (!pdMember1.getPassword().equals(s)){
            return null;
        }
        if (pdMember1.getStatus()==1){
            return null;
        }


        return this.pdMemberMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean saveMember(PdMember pdMember) {
        UUIDToDigits uuidToDigits = new UUIDToDigits();
        pdMember.setId("U"+uuidToDigits.uuidToDigits());
        pdMember.setPassword(DigestUtils.md5DigestAsHex(pdMember.getPhone().getBytes()));
        return this.pdMemberMapper.insert(pdMember)!=0;
    }

    @Override
    public int getCode(String phone) {

        SmsSample smsSample = new SmsSample();

        int code = smsSample.getCode(phone);

        return code;
    }
}
